//
//  DailyCell.swift
//  MM2
//
//  Created by 李成明 on 2022/1/1.
//

import UIKit
import Kingfisher
import CLImagePickerTool

private typealias clickClosure = (DailyCell) -> Void

class DetailContentCell: UITableViewCell {

    var model: DailyModel? {
        didSet {
            updateUI()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var avatarClickClosure:((DailyModel) -> Void)?
    
    private lazy var avatar: UIImageView = {
        let view = UIImageView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 24
        view.contentMode = .scaleAspectFit
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(avatarClickAction))
        view.addGestureRecognizer(tap)
        view.isUserInteractionEnabled = true
        return view
    }()
    
    private lazy var userName: UILabel = {
        let view = UILabel()
        view.font = UIFont(name: "Helvetica-Bold", size: 16)
        return view
    }()
    
    private lazy var followBtn: UIButton = {
        let view = UIButton()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 8
        view.layer.borderWidth = 1
        view.layer.borderColor = UIColor.initWithRGB(215, 100, 72).cgColor
        view.titleLabel?.font = .systemFont(ofSize: 16)
        view.setTitleColor(mainColor, for: .selected)
        view.setTitleColor(.initWithRGB(215, 100, 72), for: .normal)
        view.setTitle("关注", for: .normal)
        view.setTitle("已关注", for: .selected)
        view.addTarget(self, action: #selector(followClickAction), for: .touchUpInside)
        return view
    }()
    
    private lazy var contentLabel: UILabel = {
        let view = UILabel()
        view.font = .systemFont(ofSize: 15)
        view.numberOfLines = 0
        return view
    }()
    
    private lazy var imgShowView: DailyImgShowView = {
        let view = DailyImgShowView.init(frame: CGRect.zero)
        return view
    }()
    
    private lazy var bottomLine: UIView = {
        let view = UIView()
        view.backgroundColor = mainColor
        view.alpha = 0.3
        return view
    }()
}

extension DetailContentCell {
    private func setupUI() {
        contentView.addsubViews([avatar, userName, followBtn, contentLabel, imgShowView, bottomLine])
        
        avatar.frame = CGRect(x: 16, y: 16, width: 48, height: 48)
        userName.frame = CGRect(x: 16 + 48 + 8, y: 16 + 14, width: 200, height: 20)
        followBtn.frame = CGRect(x: kScreenWidth - 16 - 56, y: 16 + 9, width: 56, height: 30)
        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(contentView).offset(16 + 48 + 8)
            make.left.equalTo(contentView).offset(16)
            make.right.equalTo(contentView).offset(-16)
            make.height.equalTo(20)
        }
        
        imgShowView.snp.makeConstraints { make in
            make.left.right.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(8)
            make.bottom.equalTo(contentView).offset(-8)
            make.height.equalTo(20)
        }
        
        bottomLine.snp.makeConstraints { make in
            make.height.equalTo(0.3)
            make.left.right.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-1)
        }
    }
    
    private func updateUI() {
        guard let model = model else { return }
        avatar.setAvatar(urlStr: model.authorInfo.avatar)
        
        userName.text = model.authorInfo.userName
        
        followBtn.isHidden = model.authorInfo.userId == User.share.userId
        followBtn.isSelected = model.follow
        followBtn.layer.borderColor = model.follow ? mainColor.cgColor : UIColor.initWithRGB(215, 100, 72).cgColor
        
        contentLabel.text = model.content
        imgShowView.setImage(imgArr: model.imgArr)
        
        let textSize = contentLabel.sizeThatFits(CGSize(width: kScreenWidth - 32, height: CGFloat.greatestFiniteMagnitude))
        contentLabel.snp.updateConstraints { make in
            make.height.equalTo(textSize.height)
        }
        
        imgShowView.snp.updateConstraints { make in
            make.height.equalTo(imgShowView.viewHeight)
        }
    }
}

extension DetailContentCell {
    @objc private func followClickAction() {
        guard let model = model else { return }
        model.follow = !model.follow
        followBtn.isSelected = model.follow
        followBtn.layer.borderColor = model.follow ? mainColor.cgColor : UIColor.initWithRGB(215, 100, 72).cgColor
        
        MoyaNetwork.request(target: BaseApi.followSomeone(toUserId: model.authorInfo.userId, isFollow: model.follow)) {_,_ in }
    }
    
    @objc private func avatarClickAction() {
        guard let model = model else { return }
        avatarClickClosure?(model)
    }
    
    @discardableResult
    func avatarClick(_ closure: @escaping ((DailyModel) -> Void)) -> DetailContentCell {
        avatarClickClosure = closure
        return self
    }
}
